<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解Java锁膨胀升级机制</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: shimmer 3s infinite;
        }
        
        @keyframes shimmer {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .code-block {
            background: #2d3748;
            color: #e2e8f0;
            padding: 16px;
            border-radius: 8px;
            font-family: 'Courier New', monospace;
            overflow-x: auto;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 30px 0;
        }
        
        .floating-icon {
            position: absolute;
            opacity: 0.1;
            animation: float 6s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-20px); }
        }
        
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .list-item {
            position: relative;
            padding-left: 30px;
            margin-bottom: 12px;
        }
        
        .list-item::before {
            content: '\f054';
            font-family: 'Font Awesome 6 Free';
            font-weight: 900;
            position: absolute;
            left: 0;
            top: 3px;
            color: #667eea;
            font-size: 12px;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 relative overflow-hidden">
        <div class="floating-icon text-6xl" style="top: 20%; left: 10%;"><i class="fas fa-lock"></i></div>
        <div class="floating-icon text-8xl" style="top: 60%; right: 5%;"><i class="fas fa-cogs"></i></div>
        <div class="floating-icon text-5xl" style="bottom: 10%; left: 15%;"><i class="fas fa-code"></i></div>
        
        <div class="container mx-auto px-6 relative z-10">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 text-center">深入理解Java锁膨胀升级机制</h1>
            <p class="text-xl md:text-2xl text-center max-w-4xl mx-auto leading-relaxed">
                探索多线程并发编程中的性能优化之道，揭秘从偏向锁到重量级锁的演进历程
            </p>
            <div class="flex justify-center mt-8 space-x-4">
                <div class="bg-white bg-opacity-20 rounded-full px-6 py-3 flex items-center">
                    <i class="fas fa-clock mr-2"></i>
                    <span>阅读时间：15分钟</span>
                </div>
                <div class="bg-white bg-opacity-20 rounded-full px-6 py-3 flex items-center">
                    <i class="fas fa-layer-group mr-2"></i>
                    <span>深度：高级</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title gradient-text">什么是锁膨胀升级</h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-4 indent-8">
                随着计算机硬件性能的提升和并发处理需求的增加，多线程编程已经成为现代软件开发的常态。在Java中，锁机制是保障多线程环境下数据一致性的重要手段。然而，在我们追求高性能并发的过程中，锁的管理和使用却常常会遇到一些微妙的问题，其中一个典型的问题就是锁的膨胀升级。
            </p>
            
            <div class="highlight-box">
                <h3 class="text-2xl font-bold mb-3 flex items-center">
                    <i class="fas fa-lightbulb mr-3"></i>核心概念
                </h3>
                <p class="text-lg leading-relaxed">
                    锁膨胀升级指的是在多线程竞争激烈的场景下，锁从一种轻量级状态逐步升级为更重的状态，从而引发性能下降的现象。在Java中，锁通常有三种状态：偏向锁、轻量级锁和重量级锁。
                </p>
            </div>
        </div>

        <!-- Lock States Visualization -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h3 class="text-2xl font-bold mb-6 text-center gradient-text">锁状态演进图</h3>
            <div class="mermaid">
                graph LR
                    A[无锁状态] -->|第一个线程访问| B[偏向锁]
                    B -->|其他线程竞争| C[轻量级锁]
                    C -->|竞争激烈| D[重量级锁]
                    
                    style A fill:#e0f2fe,stroke:#0284c7,stroke-width:2px
                    style B fill:#ddd6fe,stroke:#7c3aed,stroke-width:2px
                    style C fill:#fef3c7,stroke:#f59e0b,stroke-width:2px
                    style D fill:#fee2e2,stroke:#dc2626,stroke-width:2px
            </div>
        </div>

        <!-- Lock Basics -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title gradient-text">锁的基本概念</h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6 indent-8">
                锁是并发编程中用于控制多个线程对共享资源访问的一种机制，它通过限制某一时刻仅允许一个或几个线程访问特定的资源来保证数据的完整性和一致性。
            </p>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <h3 class="text-xl font-bold mb-4 flex items-center">
                        <i class="fas fa-shield-alt mr-3 text-purple-600"></i>锁的作用
                    </h3>
                    <p class="text-gray-700">
                        锁的主要作用是确保在并发环境下，临界区的代码能够安全地执行。通过加锁，只有持有锁的线程能够进入临界区，避免了多个线程同时修改同一数据的情况。
                    </p>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-cyan-50 p-6 rounded-xl">
                    <h3 class="text-xl font-bold mb-4 flex items-center">
                        <i class="fas fa-key mr-3 text-blue-600"></i>锁的基本类型
                    </h3>
                    <div class="space-y-2">
                        <div class="list-item"><strong>互斥锁：</strong>最基本的锁类型，排他性访问</div>
                        <div class="list-item"><strong>读写锁：</strong>允许多读单写，提高并发性</div>
                        <div class="list-item"><strong>自旋锁：</strong>忙等待，适合短时间持有</div>
                        <div class="list-item"><strong>递归锁：</strong>支持同一线程多次获取</div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Lock Inflation Definition -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title gradient-text">锁膨胀升级的定义</h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6 indent-8">
                锁膨胀升级（Lock Inflation/Lock Escalation）是指在多线程环境下，由于线程竞争的激化，锁从一种较轻的状态逐步升级到更重的状态，从而导致系统开销增加、性能下降的现象。
            </p>
            
            <div class="bg-gray-50 p-6 rounded-xl mb-6">
                <h3 class="text-xl font-bold mb-4">锁的三种状态详解</h3>
                <div class="space-y-4">
                    <div class="flex items-start">
                        <div class="bg-green-500 text-white rounded-full w-8 h-8 flex items-center justify-center mr-4 flex-shrink-0">1</div>
                        <div>
                            <h4 class="font-bold text-lg mb-2">偏向锁（Biased Locking）</h4>
                            <p class="text-gray-700">适用于无竞争或极低竞争的场景，锁的持有者可以反复进入和退出临界区而不需要实际的同步操作。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start">
                        <div class="bg-yellow-500 text-white rounded-full w-8 h-8 flex items-center justify-center mr-4 flex-shrink-0">2</div>
                        <div>
                            <h4 class="font-bold text-lg mb-2">轻量级锁（Lightweight Locking）</h4>
                            <p class="text-gray-700">使用CAS操作来尝试获取锁，适用于少量竞争的场景，避免了线程的阻塞和操作系统的调度。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start">
                        <div class="bg-red-500 text-white rounded-full w-8 h-8 flex items-center justify-center mr-4 flex-shrink-0">3</div>
                        <div>
                            <h4 class="font-bold text-lg mb-2">重量级锁（Heavyweight Locking）</h4>
                            <p class="text-gray-700">通过操作系统的同步机制实现，线程在争夺锁时会被阻塞，导致线程上下文切换等系统开销。</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Causes of Lock Inflation -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title gradient-text">锁膨胀升级的原因</h2>
            
            <div class="grid md:grid-cols-2 gap-6 mb-6">
                <div class="bg-gradient-to-r from-red-50 to-orange-50 p-6 rounded-xl">
                    <h3 class="text-xl font-bold mb-4 flex items-center">
                        